package com.example.gameapplication.bullet;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;

import com.example.gameapplication.R;

public class DoubleBullet extends Bullet {

    private Bitmap mBullet;
    private boolean leftIsAlive;
    private boolean rightIsAlive;
    public float leftx2;
    public float lefty2;
    public float rightx2;
    public float righty2;

    public boolean isLeftIsAlive() {
        return leftIsAlive;
    }

    public void setLeftIsAlive(boolean leftIsAlive) {
        this.leftIsAlive = leftIsAlive;
    }

    public boolean isRightIsAlive() {
        return rightIsAlive;
    }

    public void setRightIsAlive(boolean rightIsAlive) {
        this.rightIsAlive = rightIsAlive;
    }

    @Override
    public boolean isAlive() {
        return leftIsAlive || rightIsAlive;
    }

    @Override
    public void setAlive(boolean alive) {
        leftIsAlive = rightIsAlive = alive;
    }

    public DoubleBullet(Resources resources, Paint paint) {
        super(resources, paint);
    }

    @Override
    public void initBitmap() {
        if (mBullet != null && !mBullet.isRecycled()) mBullet.recycle();
        //子弹外形
        switch (getLv()) {
            case 1:
                mBullet = BitmapFactory.decodeResource(resources, R.drawable.my_bullet_blue);
                break;
            case 2:
                mBullet = BitmapFactory.decodeResource(resources, R.drawable.my_bullet_purple);
                break;
            case 3:
                mBullet = BitmapFactory.decodeResource(resources, R.drawable.my_bullet_red);
                break;
            case 4:
                mBullet = BitmapFactory.decodeResource(resources, R.drawable.boss_bullet_hellfire_yellow);
                break;
            default:mBullet = BitmapFactory.decodeResource(resources, R.drawable.boss_bullet_hellfire_red);
                break;
        }
        object_width = mBullet.getWidth();
        object_height = mBullet.getHeight();
    }

    @Override
    public void drawSelf(Canvas canvas) {//子弹的飞行逻辑
        if (!isAlive()) return;
        setSeat(midx, midy);
        //画左边子弹
        if (leftIsAlive) {
            canvas.save();
            canvas.clipRect(leftx, lefty, rightx, righty);
            canvas.drawBitmap(mBullet, leftx, lefty, paint);
            canvas.restore();
        }
        //画右边子弹
        if (rightIsAlive) {
            canvas.save();
            canvas.clipRect(leftx2, lefty2, rightx2, righty2);
            canvas.drawBitmap(mBullet, leftx2, lefty2, paint);
            canvas.restore();
        }
        logic();
    }

    public void logic() {//子弹的逻辑，产生之后，x不变，y轴变化
        if (midy < 0) {//离开屏幕
            setAlive(false);
            return;
        }
        if (isAlive())//子弹击中目标，则在离开屏幕之前就被销毁了
            midy = midy - speed;
    }

    //更新该对象的位置，传入中心点的坐标
    public void setSeat(float x, float y) {//与普通子弹不同
        midx = x;
        midy = y;
        leftx = midx - object_width * 7 / 6;
        lefty = midy - object_height / 2;
        rightx = leftx + object_width;
        righty = lefty + object_height;
        leftx2 = midx + object_width / 6;
        lefty2 = lefty;
        rightx2 = leftx2 + object_width;
        righty2 = lefty2 + object_height;
    }
}
